package com.yanqu.road.server.cmd.activity.peakstronghold;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.ePeakStrongholdStageType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.logic.rank.UnionRankMsgModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PeakStrongholdWarProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.CommonActivityRankMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Map;
import java.util.Objects;

@Cmd(code = GameProtocol.S_PEAK_STRONGHOLD_UNION_ABILITY_RANK, desc = "报名期排行")
public class PeakStrongholdUnionAbilityRankCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PeakStrongHoldConfig config = PeakStrongholdActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (config.getStageType() != ePeakStrongholdStageType.Registration) {
            ret = GameErrorCode.E_PEAK_HOLD_NOT_IN_ELIMINATE;
            return;
        }
//        PeakStrongholdWarProto.CrossPeakHoldMemberSilverDonateReq.Builder crossMsg = PeakStrongholdWarProto.CrossPeakHoldMemberSilverDonateReq.newBuilder();
//        crossMsg.setActivityId(config.getActivityInfo().getActivityId());
//        crossMsg.setUnionId(player.getUserInfo().getUnionUid());
//        player.sendPacket(Protocol.C_PEAK_STRONGHOLD_UNION_ABILITY_RANK, crossMsg);

        int activityId = config.getActivityInfo().getActivityId();
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (null == activityInfo) {
            return;
        }
        int ownRank = 0;
        int rank = 1;
        String myValue = "0";
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        boolean isGroupType = UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType());

        PeakStrongholdWarProto.PeakStrongholdUnionAbilityRankRespMsg.Builder respMsg = PeakStrongholdWarProto.PeakStrongholdUnionAbilityRankRespMsg.newBuilder();
        UnionRankMsgModel model = CommonActivityRankMgr.getUnionRankMsgModelByGroup(activityId, groupId);
        if (null != model) {
            boolean isInside = false;
            String playerUnionId = player.getUserInfo().getUnionUid();
            if (isGroupType) {
                playerUnionId = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
            }
            RankProto.UnionRankListMsg rankListMsg = model.getRankListMsg();
            for (RankProto.UnionRankTempMsg rankTempMsg : rankListMsg.getRankListList()) {
                PeakStrongholdWarProto.UnionAbilityRankTemp.Builder rankMsg = PeakStrongholdWarProto.UnionAbilityRankTemp.newBuilder();
                rankMsg.setRank(rank++);
                rankMsg.setUnionInfo(rankTempMsg.getUnionBaseData());
                rankMsg.setAbility(rankTempMsg.getValue());
                rankMsg.setUnionId(rankTempMsg.getUnionUid());
                respMsg.addRankData(rankMsg);

                if (rankTempMsg.getUnionUid().equals(playerUnionId)) {
                    ownRank = rankMsg.getRank();
                    myValue = rankMsg.getAbility();
                    isInside = true;
                }
            }

            if (!isInside && !StringUtils.isNullOrEmpty(playerUnionId)) {//不在榜单里,累计游戏服缓存的赚钱
                Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(playerUnionId);
                BigInteger unionTotalValue = BigInteger.ZERO;
                for (CrossUnionMember member : memberMap.values()) {
                    unionTotalValue = unionTotalValue.add(member.getHistoryMaxAbility());
                }
                myValue = unionTotalValue.toString();
            }

            respMsg.setValue(myValue);
            respMsg.setOwnRank(ownRank);
            player.sendPacket(Protocol.U_PEAK_STRONGHOLD_UNION_ABILITY_RANK, respMsg);

            if (model.isNeedUpdate()) {
                model.addSynPlayer(player.getUserId());
                if (!model.isInReq()) {
                    //向商会跨服请求更新
                    CrossUnionProto.CrossUnionGetMaxAbilityUnionRankListReq.Builder crossReqMsg = CrossUnionProto.CrossUnionGetMaxAbilityUnionRankListReq.newBuilder();
                    crossReqMsg.setActivityId(activityId);
                    crossReqMsg.addAllServerIds(activityInfo.getServerIdList());
                    crossReqMsg.setRankNum(model.getRankNum());
                    crossReqMsg.setActivityEndTime(activityInfo.getEndShowTime());
                    crossReqMsg.setGroupId(model.getGroupId());
                    GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_UNION_GET_MAX_ABILITY_UNION_RANK_LIST, crossReqMsg));
                }
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
